﻿//-----------------------------------------------------------------------
// <copyright file="GetKey.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Mike Fourie [Revised Task]</author>
// <email>v-mifour@microsoft.com</email>
// <date>2008-02-01</date>
// <summary>Gets a Registry Key Value</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.Registry
{
    using System;
    using Microsoft.Sdc.Tasks;
    using Microsoft.Win32;
    using Microsoft.Build.Framework;
    
    /// <summary>
    /// Gets a Registry Key Value.
    /// </summary>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Registry.GetKey MachineName="AMachineName" RegistryHive="LocalMachine" Key="SOFTWARE\Microsoft\Windows\CurrentVersion" Value="ProgramFilesDir">
    ///             <Output TaskParameter="Data" PropertyName="RegValue"/>
    ///         </Registry.GetKey>
    ///         <Message Text="The reg value is $(RegValue)" />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    public class GetKey : TaskBase
    {
        private string data;
        private string key;
        private string machineName;
        private string registryHive;
        private string value;

        /// <summary>
        /// Gets or sets the data.
        /// </summary>
        /// <value>The Key value.</value>
        [Output]
        public string Data
        {
            get
            {
                return this.data;
            }

            set
            {
                this.data = value;
            }
        }

        /// <summary>
        /// Gets or sets the registry hive.
        /// </summary>
        /// <value>The registry hive.</value>
        [Required]
        public string RegistryHive
        {
            get
            {
                return this.registryHive;
            }

            set
            {
                this.registryHive = value;
            }
        }

        /// <summary>
        /// Gets or sets the name of the machine.
        /// Defaults to local machine if not specified.
        /// </summary>
        /// <value>The name of the machine.</value>
        public string MachineName
        {
            get
            {
                return (this.machineName ?? String.Empty);
            }

            set
            {
                this.machineName = value;
            }
        }

        /// <summary>
        /// Gets or sets the value. If Value is not provided, an attempt will be made to read the Default Value.
        /// </summary>
        /// <value>The value.</value>
        public string Value
        {
            get
            {
                return (this.value ?? String.Empty);
            }

            set
            {
                this.value = value;
            }
        }

        /// <summary>
        /// Gets or sets the key.
        /// </summary>
        /// <value>The key.</value>
        [Required]
        public string Key
        {
            get
            {
                return this.key;
            }

            set
            {
                this.key = value;
            }
        }
        
        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            RegistryKey registryKey;

            try
            {
                Microsoft.Win32.RegistryHive hive = (Microsoft.Win32.RegistryHive)Enum.Parse(typeof(Microsoft.Win32.RegistryHive), this.RegistryHive, true);
                registryKey = RegistryKey.OpenRemoteBaseKey(hive, this.MachineName);
            }
            catch (System.ArgumentException)
            {
                Log.LogError(string.Format("The Reqistry Hive provided is not valid: {0}", this.RegistryHive));
                return;
            }

            RegistryKey subKey = registryKey.OpenSubKey(this.Key, false);
            if (subKey == null)
            {
                Log.LogError(string.Format("The Reqistry Key provided is not valid: {0}", this.Key));
                return;
            }

            object v = subKey.GetValue(this.Value);
            if (v == null)
            {
                if (string.IsNullOrEmpty(this.Value))
                {
                    Log.LogError(string.Format("A Default value was not found for the Registry Key: {0}", this.Key));
                }
                else
                {
                    Log.LogError(string.Format("The Reqistry Value provided is not valid: {0}", this.Value));
                }
                return;
            }
            else
            {
                this.data = v.ToString();
            }

            subKey.Close();
            registryKey.Close();
        }
    }
}
